package cn.edu.zut.salarymanagesystem.bean.vo;

import java.util.ArrayList;
import java.util.List;

import cn.edu.zut.salarymanagesystem.bean.pojo.Append;
import cn.edu.zut.salarymanagesystem.bean.pojo.WorkRecord;



public class WorksVO {
    private List<WorkRecord> works = new ArrayList<WorkRecord>();
    private Integer total = 0; //查询结果的总工资
    private Integer rangedTotal = 0; //当前请求页内的总工资
    private Integer itemsNum = 0;   //工资列表的大小

    public List<WorkRecord> getWorks() {
        return works;
    }

    public void setWorks(List<WorkRecord> works) {
        this.works = works;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public Integer getItemsNum() {
        return itemsNum;
    }

    public void setItemsNum(Integer itemsNum) {
        this.itemsNum = itemsNum;
    }

    public static int sumTotal(List<WorkRecord> list) {
        int total = 0;
        for (WorkRecord wr : list) {
            if (wr instanceof Append) {
                if (((Append) wr).getValid() == 0) {
                    continue;
                }
            }
            total += wr.getValue();
        }
        return total;
    }

    public static void paged(WorksVO vo, int pageNum, int pageSize) {
        int size = vo.getWorks().size();
        if (size >= pageNum * pageSize) {
            vo.setWorks(vo.getWorks().subList((pageNum - 1) * pageSize, (pageNum - 1) * pageSize + pageSize));
        } else if (pageNum * pageSize > size && (pageNum - 1) * pageSize < size) {
            vo.setWorks(vo.getWorks().subList((pageNum - 1) * pageSize, size));
        } else {
            vo.setWorks(new ArrayList<>());
        }
        vo.setRangedTotal(WorksVO.sumTotal(vo.getWorks()));
    }

    public Integer getRangedTotal() {
        return rangedTotal;
    }

    public void setRangedTotal(Integer rangedTotal) {
        this.rangedTotal = rangedTotal;
    }

    @Override
    public String toString() {
        return "WorksVO{" +
                "works=" + works +
                ", total=" + total +
                ", rangedTotal=" + rangedTotal +
                ", itemsNum=" + itemsNum +
                '}';
    }
}
